home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld Secrets (4th Edition)
/
Mac Secrets CD 4th Ed.toast
/
Apple Advanced Technologies
/
Apple Speech Technologies 1.5
/
PlainTalk Developer Info
/
Speech Recognition Manager SDK
/
SR Sample Code
/
IM SR Example
/
main.c
next >
Wrap
Text File
|
1996-09-18
|
7KB
|
290 lines
// Speech Recognition Example
// combines routines from IM document into application
// uses MetroWerks' SIOUX window
#include <SpeechRecognition.h>
#include <AppleEvents.h>
#include <Devices.h>
#include <Dialogs.h>
#include <Events.h>
#include <Fonts.h>
#include <Quickdraw.h>
#include <Menus.h>
#include <Resources.h>
#include <TextEdit.h>
#include <ToolUtils.h>
#include <Windows.h>
#include <SIOUX.h>
#include <stdio.h>
#include <stdlib.h>
// define this to be true if you want to use SR callbacks
#define SR_CALLBACKS 0
#define rMenuBar 128 /* Menubar resource id */
#define mApple 128 /* Apple menu resource id */
#define mFile 129 /* File menu resource id */
// External Prototypes
extern SRRecognitionSystem MyInitSpeechRecognition (void);
extern SRLanguageModel MyBuildLanguageModel (SRRecognitionSystem mySystem);
extern OSErr MyInstallSpeechAEHandlers (void);
extern pascal OSErr MyInstallNotificationCallBack (SRRecognizer recognizer);
extern pascal void MyRemoveNotificationCallBack (SRRecognizer recognizer);
extern pascal void MyIdleCheckForSpeechResult (void);
// Internal Prototypes
Boolean Initialize (void);
void EventLoop (void);
void Terminate (void);
void HandleMouseDown (EventRecord * event);
void HandleKeyDown (unsigned char keyChar);
void HandleMenuSelect (long menuChoice);
pascal OSErr DummyAEHandler (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
pascal OSErr HandleQuitAE (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
// Globals
Boolean gFinished = false;
SRRecognitionSystem gRecSystem = NULL;
SRLanguageModel gLanguageModel = NULL;
SRRecognizer gRecognizer = NULL;
Boolean gListening = false;
// Routines
void main (void)
{
if ((gListening = Initialize ()) == true)
EventLoop ();
else
{
SysBeep (0);
printf ("Initialization failed.\n");
}
Terminate ();
}
Boolean Initialize (void)
{
Boolean successfulStartup = false;
OSErr myErr = noErr;
short index;
EventRecord event;
InitGraf((Ptr) &qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
for (index = 1; index <= 3; index++)
EventAvail(everyEvent, &event);
if (!myErr)
myErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
NewAEEventHandlerProc (DummyAEHandler), 0, false);
if (!myErr)
myErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
NewAEEventHandlerProc (DummyAEHandler), 0, false);
if (!myErr)
myErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
NewAEEventHandlerProc (DummyAEHandler), 0, false);
if (!myErr)
myErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
NewAEEventHandlerProc (HandleQuitAE), 0, false);
if (!myErr)
{
Handle menuBar = GetNewMBar(rMenuBar);
if (menuBar != nil)
{
SetMenuBar(menuBar);
DisposHandle(menuBar);
AddResMenu(GetMenuHandle(mApple), 'DRVR'); // add DA names to Apple menu
DrawMenuBar();
}
else
myErr = (ResError() ? ResError() : 1);
}
SIOUXSettings.standalone = false;
SIOUXSettings.setupmenus = false;
SIOUXSettings.initializeTB = false;
SIOUXSettings.columns = 90;
printf("Initializing.\n");
SIOUXSetTitle ("\pSpeech Recognition Example");
if ((gRecSystem = MyInitSpeechRecognition ()) == NULL)
{
myErr = true;
printf("Initialization failed.\n");
}
if (!myErr)
if ((gLanguageModel = MyBuildLanguageModel (gRecSystem)) == NULL)
myErr = true;
if (!myErr)
{
printf("\n");
printf("This program uses a language model with following BNF:\n");
printf("<TopLM> = <call> <person> | schedule meeting with <person> | view today's schedule;\n");
printf("<call> = call | phone | dial;\n");
printf("<person>= Arlo | Matt | Brent | my wife;\n");
printf("\n");
}
if (!myErr)
myErr = SRNewRecognizer (gRecSystem, &gRecognizer, kSRDefaultSpeechSource);
if (!myErr)
myErr = MyInstallSpeechAEHandlers ();
#if SR_CALLBACKS
if (!myErr)
myErr = MyInstallNotificationCallBack (gRecognizer);
#endif
if (!myErr)
myErr = SRSetLanguageModel (gRecognizer, gLanguageModel);
if (!myErr)
myErr = SRStartListening (gRecognizer);
if (!myErr)
successfulStartup = true;
if (!successfulStartup)
printf("An error occured, quitting...\n");
return successfulStartup;
}
void Terminate (void)
{
printf("Terminating.\n");
if (gListening) SRStopListening (gRecognizer);
if (gRecognizer)
{
#if SR_CALLBACKS
MyRemoveNotificationCallBack (gRecognizer);
#endif
SRReleaseObject (gRecognizer);
}
if (gLanguageModel) SRReleaseObject (gLanguageModel);
if (gRecSystem) SRCloseRecognitionSystem (gRecSystem);
}
void EventLoop (void)
{
while (!gFinished)
{
EventRecord event;
unsigned char keyChar;
if (WaitNextEvent(everyEvent, &event, 1, nil))
{
// only handle event if SIOUX doesn't
if (!SIOUXHandleOneEvent(&event))
switch (event.what)
{
case mouseDown :
HandleMouseDown (&event);
break;
case keyDown :
keyChar = event.message & charCodeMask;
if (event.modifiers & cmdKey)
HandleMenuSelect (MenuKey(keyChar));
break;
case kHighLevelEvent :
AEProcessAppleEvent(&event);
break;
default :
break;
}
}
#if SR_CALLBACKS
MyIdleCheckForSpeechResult ();
#endif
}
}
void HandleMouseDown (EventRecord * event)
{
WindowPtr window;
short partCode = FindWindow (event->where, &window);
switch (partCode)
{
case inSysWindow :
SystemClick (event, window);
break;
case inContent:
break;
case inMenuBar :
HandleMenuSelect (MenuSelect(event->where));
break;
case inDrag :
DragWindow (window, event->where, &qd.screenBits.bounds);
break;
case inGoAway:
if (TrackGoAway (window, event->where))
gFinished = true;
break;
default :
break;
}
HiliteMenu (0);
}
void HandleMenuSelect (long menuChoice)
{
short itemNum;
Str255 daName;
GrafPtr ourPort;
itemNum = LoWord(menuChoice);
switch (HiWord(menuChoice))
{
case mApple :
GetItem (GetMenuHandle(mApple), itemNum, daName);
GetPort (&ourPort);
OpenDeskAcc (daName);
SetPort (ourPort);
break;
case mFile :
// all we've got is quit
gFinished = true;
break;
default :
break;
}
}
pascal OSErr DummyAEHandler (AppleEvent */*theAEevt*/, AppleEvent* /*reply*/, long /*refcon*/)
{
return noErr;
}
pascal OSErr HandleQuitAE (AppleEvent */*theAEevt*/, AppleEvent* /*reply*/, long /*refcon*/)
{
gFinished = true;
return noErr;
}